package com.lv.jie.service.impl;

import com.lv.jie.mapper.HotMapper;
import com.lv.jie.service.HotService;
import com.lv.jie.utils.JsonResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author 24694
 */
@Service("HotService")
public class HotServiceImpl implements HotService {
    final HotMapper hotMapper;
    final RedisTemplate redisTemplate;

    public HotServiceImpl(HotMapper hotMapper, RedisTemplate redisTemplate) {
        this.hotMapper = hotMapper;
        this.redisTemplate = redisTemplate;
    }

    @Override
    public JsonResult likeAddOne(Integer hotId, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        int i = hotMapper.updateByPrimaryKey(hotId);
        if (i == 0){
            return new JsonResult("服务器错误",500);
        }
        return new JsonResult("点赞成功");
    }

    private boolean getToken(String token){
        boolean flag = false;
        Object redisCode = redisTemplate.opsForValue().get(token);
        if (redisCode != null){
            flag = true;
        }
        return !flag;
    }
}
